package cn.jdy.plugins.wap;





import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.ApplyCash;
import cn.jdy.plugins.entity.CRateOrder;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.entity.MemberCard;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.ApplyCashService;
import cn.jdy.plugins.service.MemberCardService;
import cn.jdy.plugins.service.MemberService;

/**
 * @title  银行卡管理
 * @author jga
 * @since  2018-10-13
 */

@Controller
@RequestMapping("/memberCard")
public class MemberCardCtrl extends BaseCtrl{

	@Autowired
	private MemberCardService memberCardService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ApplyCashService applyCashService;
	/**
	 * 我的银行卡
	 *
	 * @author: wuzhiliang
	 * @date: 2013-10-15 16:53
	 */
	@RequestMapping("/index")
	public ModelAndView indexsz(MemberCard order) {
		ModelAndView mv = new ModelAndView("/member/changeCard.jsp");
		try {
			Member member = super.getMember(true);
			
			mv.addObject("member", memberService.find(member.getId()));
		List<MemberCard> list=	memberCardService.find("memberid",order.getMemberid());
			if(list.size()>0){
				MemberCard  mc=list.get(0);
				mv.addObject("mc", mc);
			}
	
		
		} catch(Exception e) {
			mv.addObject("msg", "error");
			BaseLog.e(this.getClass(), "银行卡", e);
		}
		return mv;
	}
	/**
	 * @title : 保存会员卡银行信息
	 * @description: 保存会员卡银行信息
	 * @author: jga
	 * @date: 
	 */
	@RequestMapping("/forSaveCard")
    @ResponseBody
    public Map<String, Object> forSaveCard(MemberCard sp,String authcode,String txpassd) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(null == request.getSession().getAttribute(GlobalConstant._PHONECODE)||!authcode.equals(request.getSession().getAttribute(GlobalConstant._PHONECODE))){
			jsonMap.put("msg", "短信验证码无效！");
				return jsonMap;
		}
 
		try {
			Member member = super.getMember(true);
			if(sp.getId()==null) {//
		
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				sp.setMemberid(member.getId());
				sp.setMembername(member.getUserName());
				sp.setStatus("0");
				memberCardService.save(sp);
				Member mb=memberService.find(member.getId());
				mb.setTxpassd(txpassd);
				memberService.update(mb);
			} else {//更新
				sp.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			 
				memberCardService.update(sp);
				Member mb=memberService.find(member.getId());
				mb.setTxpassd(txpassd);
				memberService.update(mb);
			}
	 
			jsonMap.put("msg", "200");
		} catch(BaseException e) {
			jsonMap.put("msg", "500");
			jsonMap.put("msgmsg", "失败！");
			BaseLog.e(this.getClass(), "保存会员注册信息错误", e);
		}
		return jsonMap;
	}

	  /**
	   * 获取我的提现记录
	   * @param  
	   * @param key
	   * @param pid
	   * @return
	   */
	   @RequestMapping("myTixianlist")
	   public ModelAndView myTixianlist(){
	     ModelAndView mv = new ModelAndView("/member/myTixianlist.jsp");
 
	 /*    ApplyCash  ane=new ApplyCash();
		Member member = super.getMember(true);
	     ane.setUserOpenId(member.getId());
 
	     ane.setiDisplayStart(0);//  
	     ane.setiDisplayLength(6);//  
	     mv.addObject("dataList", applyCashService.query(ane)); //
	    // mv.addObject("pid",productx.getColumnId());*/
	     return mv;
	   }
	   /**
		 * 加载列表
		 * 
		 * @param rpSendId
		 * @return
		 */
		@RequestMapping("getmyTixianlist")
		@ResponseBody
		public Map<String, Object> getmyTixianlist(int pageNo, int pageSize) {
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			Long count = 0L;
			List<ApplyCash> list = new ArrayList<ApplyCash>();
			DetachedCriteria dc = DetachedCriteria.forClass(ApplyCash.class);
			Member member = super.getMember(true);
			if (!StringUtil.isNullOrEmpty(member.getId())) {
				dc.add(Restrictions.eq("userOpenId", member.getId()));
			}
		 
			dc.addOrder(Order.asc("createTime"));

			list = applyCashService.find(dc,pageNo, pageNo+pageSize);
			count = applyCashService.count(dc);
			jsonMap.put("datalist", list);
			jsonMap.put("iTotalDisplayRecords", count);
			return jsonMap;
		}
}
